//
//  WBBaseViewController.swift
//  仿写微博
//
//  Created by Apple on 17/6/30.
//  Copyright © 2017年 tuimao. All rights reserved.
//

import UIKit
//基类
class WBBaseViewController: UIViewController {
    //访客视图字典
    var visitorInfoDict:[String:String]?
    //创建表视图
    var tableView:UITableView?
    //懒加载添加刷新控件
    lazy var refreshControl:CZRefreshControl = CZRefreshControl()
    //是否上拉刷新
    var isPullup = false
    //自定义navigationBar
    lazy var navigationBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: UIScreen.cz_screenWidth(), height: 64))
    //自定义UINavigationItem
    lazy var navItem = UINavigationItem()
    override func viewDidLoad() {
        super.viewDidLoad()
        //加载UI
        setupUI()
        //加载数据
        WBNetworkManager.shared.userlogon ? loadData() : ()
        //登录成功的通知
        NotificationCenter.default.addObserver(self, selector: #selector(loginSuccess), name: NSNotification.Name(rawValue: WBUserLoginSuccessNotification), object: nil)
    }
    deinit {
        //移除通知
        NotificationCenter.default.removeObserver(self)
    }
    
    //重写title
    override var title: String?{
        didSet{
            navItem.title = title
        }
    }
    //加载数据 - 具体数据由子类去实现
    func loadData() {
        //子类不实现方法，默认关闭刷新
        refreshControl.endRefreshing()
    }
}
//MARK:访客视图的登录、注册方法
extension WBBaseViewController{
    //注册
    @objc fileprivate func regist(){
        print("注册")
    }
    //登录
    @objc fileprivate func login(){
        //发送登录通知
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: WBUserShouldLoginNotification), object: nil)
    }
    
    @objc fileprivate func loginSuccess(n: Notification){
        navItem.leftBarButtonItem = nil
        navItem.rightBarButtonItem = nil
        //更新UI
        view = nil
        //移除通知
        NotificationCenter.default.removeObserver(self)
    }
}


//MARK: 创建UI
extension WBBaseViewController{
   fileprivate func setupUI(){
        view.backgroundColor = UIColor.white
        //取消tableview的自动缩进
        automaticallyAdjustsScrollViewInsets = false
        //创建自定义导航条
        setupNav()
        //创建表视图
        WBNetworkManager.shared.userlogon ? setupTableView() : setupVisitorView()
    }
    //MARK:创建访客视图
   fileprivate func setupVisitorView() {
        let visitorView = WBVisitorView(frame: view.bounds)
        //把访客视图
        visitorView.visitorInfor = visitorInfoDict
        //访客视图中的登录注册按钮的点击方法
        visitorView.loginButton.addTarget(self, action: #selector(login), for: .touchUpInside)
        visitorView.registButton.addTarget(self, action: #selector(regist), for: .touchUpInside)
        //添加tabbatItem
        navItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: .plain, target: self, action: #selector(regist))
        navItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style: .plain, target: self, action: #selector(login))
        //添加到视图上
        view.insertSubview(visitorView, belowSubview: navigationBar)
    }
    //MARK: 创建自定义导航条
   fileprivate func setupNav(){
        //添加自定义导航条
        view.addSubview(navigationBar)
        //把item设置给bar
        navigationBar.items = [navItem]
        //设置NAVbar的渲染颜色
        navigationBar.barTintColor = UIColor.cz_color(withHex: 0xF6F6F6)
        //设置navBar的字体颜色
        navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.darkGray]
        //设置navBarItem的按钮颜色
        navigationBar.tintColor = UIColor.orange
    }
    //MARK:创建表视图
    func setupTableView() {
        tableView = UITableView(frame: view.bounds, style: .plain)
        view.insertSubview(tableView!, belowSubview: navigationBar)
        tableView?.delegate = self
        tableView?.dataSource = self
        //设置内容缩进
        tableView?.contentInset = UIEdgeInsets(top: navigationBar.bounds.height, left: 0, bottom: tabBarController?.tabBar.bounds.height ?? 49, right: 0)
        //设置指示器缩进
        tableView?.scrollIndicatorInsets = tableView!.contentInset
        //添加刷新控件
        tableView?.addSubview(refreshControl)
        refreshControl.addTarget(self, action: #selector(loadData), for: .valueChanged)
    }
}
//MARK:TableViewDelegate,UITableViewDataSource
extension WBBaseViewController:UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 10
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return UITableViewCell()
    }
    //即将展现的cell，用来判断是否上拉刷新
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        
        let row = indexPath.row
        let section = tableView.numberOfSections - 1
        if row < 0 || section < 0 {
            return
        }
        let count = tableView.numberOfRows(inSection: section)
        if row == (count - 1) && !isPullup {
            print("上拉刷新")
            isPullup = true
            loadData()
        }
        
    }
    
}



